#include "pressed_event.h"
#include "cmsis_os2.h"
void F1Init(void){
    IoTGpioInit(F1_GPIO);
    IoTGpioSetFunc(F1_GPIO, IOT_GPIO_FUNC_GPIO_11_GPIO);
    IoTGpioSetDir(F1_GPIO, IOT_GPIO_DIR_IN);
    IoTGpioSetPull(F1_GPIO, IOT_GPIO_PULL_UP);
}
void F2Init(void){
    IoTGpioInit(F2_GPIO);
    IoTGpioSetFunc(F2_GPIO, IOT_GPIO_FUNC_GPIO_12_GPIO);
    IoTGpioSetDir(F2_GPIO, IOT_GPIO_DIR_IN);
    IoTGpioSetPull(F2_GPIO, IOT_GPIO_PULL_UP);
}
// 消除按键抖动
void WrapCallBack(char* timerId){
    osTimerStart(timerId,10);
}
void TimerHandler(void *arg){
    event_t* event = (event_t*)arg;
    IotGpioValue value;
    IoTGpioGetInputVal(event->gpio, &value);
    if(value == IOT_GPIO_VALUE0){
        event->func(NULL);
    }
}
void SetF1PressedEvent(GpioIsrCallbackFunc callback){
    event_t *event = (event_t*)malloc(sizeof(event_t));
    event->gpio = F1_GPIO;
    event->func = callback;
    osTimerId_t timer = osTimerNew(TimerHandler, osTimerOnce, event,NULL);
    IoTGpioRegisterIsrFunc(F1_GPIO, IOT_INT_TYPE_EDGE, IOT_GPIO_EDGE_FALL_LEVEL_LOW, WrapCallBack, timer);
}
void SetF2PressedEvent(GpioIsrCallbackFunc callback){
    event_t *event = (event_t*)malloc(sizeof(event_t));
    event->gpio = F2_GPIO;
    event->func = callback;
    osTimerId_t timer = osTimerNew(TimerHandler, osTimerOnce, event,NULL);
    IoTGpioRegisterIsrFunc(F2_GPIO, IOT_INT_TYPE_EDGE, IOT_GPIO_EDGE_FALL_LEVEL_LOW, WrapCallBack, timer);
}
